local xingshuai = fk.CreateSkill {

  name = "joy__xingshuai$",

  tags = { Skill.Limited, },

}



xingshuai:addEffect(fk.EnterDying, {
  name = "joy__xingshuai$",
  anim_type = "defensive",
  frequency = Skill.Limited,
  events = {fk.EnterDying},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(xingshuai.name) and player:usedSkillTimes(xingshuai.name, Player.HistoryGame) == 0 and
      not table.every(player.room:getOtherPlayers(player), function(p) return p.kingdom ~= "wei" end)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local targets = {}
    for _, p in ipairs(room:getOtherPlayers(player)) do
      if p.kingdom == "wei" and room:askForSkillInvoke(p, xingshuai.name, data, "#joy__xingshuai-invoke::"..player.id) then
        table.insert(targets, p)
      end
    end
    if #targets > 0 then
      for _, p in ipairs(targets) do
        room:recover{
          who = player,
          num = 1,
          recoverBy = p,
          skillName = xingshuai.name
        }
      end
    end
    if not player.dying then
      for _, p in ipairs(targets) do
        room:damage{
          to = p,
          damage = 1,
          skillName = xingshuai.name,
        }
        if not p.dead then
            room:drawCards(p,1,xingshuai.name)
        end
      end
    end
  end,
})

return xingshuai